package cn.byapps.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.byapps.entity.User;
import cn.byapps.service.UserService;

@Controller
@RequestMapping("admin")
public class GenealogyAdminController {

	@Resource
	private UserService userService;
	@Value("${admin}")
	private String admin;
	@Value("${password}")
	private String password;

	@RequestMapping(value = { "login", "/", "" }, method = RequestMethod.GET)
	public String login() {
		return "admin/login";
	}

	@RequestMapping(value = { "index" }, method = RequestMethod.GET)
	public String index() {
		return "admin/index";
	}

	@RequestMapping(value = { "change-password" }, method = RequestMethod.GET)
	public String getChangePassword(Integer id, HttpSession session) {
		if (id != null) {
			User u = userService.get(id.toString());
			session.setAttribute("user_pwd", u);
		} else {
			session.removeAttribute("user_pwd");
		}
		return "admin/change-password";
	}

	@RequestMapping(value = { "change-password" }, method = RequestMethod.POST)
	@ResponseBody
	public Integer changePassword(Integer id, String pwd, HttpSession session) {
		session.removeAttribute("user_pwd");
		if (id != null) {
			User u = new User();
			u.setId(id);
			boolean b = userService.changePassword(u, pwd);

			return 1;
		}
		return -1;
	}

	@RequestMapping(value = { "stopUser" }, method = RequestMethod.POST)
	@ResponseBody
	public Integer stopUser(Integer id, HttpSession session) {
		if (id != null) {
			User u = new User();
			u.setId(id);
			boolean b = userService.deleteByAdmin(u);
			if (b)
				return 1;
			else
				return 0;
		}
		return -1;
	}

	@RequestMapping(value = { "startUser" }, method = RequestMethod.POST)
	@ResponseBody
	public Integer startUser(Integer id, HttpSession session) {
		if (id != null) {
			User u = new User();
			u.setId(id);
			boolean b = userService.startByAdmin(u);
			if (b)
				return 1;
			else
				return 0;
		}
		return -1;
	}

	@RequestMapping(value = { "member-list" })
	public String list(String username, HttpSession session) {
		List<User> users = null;
		if (username == null || username.equals(""))
			users = userService.findAll();
		else
			users = userService.findByName(username);
		session.setAttribute("users", users);
		return "admin/member-list";
	}

	@RequestMapping(value = { "login" }, method = RequestMethod.POST)
	public String login(String name, String password) {
		if (name != null && password != null && name.equals(admin) && password.equals(this.password))
			return "redirect:index";
		else
			return "admin/login";
	}
}
